﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Practices.Unity;
using Iacym.Entities;
using Iacym.BusinessLogic;

namespace Iacym.UI
{
    public partial class FrmMinisterio : Form
    {
        [Dependency]
        public IMinisterioBL MinisterioBL { get; set; }

        [Dependency]
        public IUsuarioMenuBL usuMenuBL { get; set; }

        public FrmMinisterio()
        {
            InitializeComponent();
            this.BuildUp();
        }


        bool isExtend;

        public void Extend()
        {
            isExtend = true;
        }

        public USUARIO currentUsuarioMinisterio { get; set; }

        private void FrmMinisterio_Load(object sender, EventArgs e)
        {
            if (currentUsuarioMinisterio != null)
            {
                menuLlena();
            }
            else
                CargaData();
        }

        public void menuLlena()
        {
            var datoUsuario = usuMenuBL.GetAllFromUsuarioByPersonaFromLogin(currentUsuarioMinisterio.PERSONA.Dni);

            var permisos = (from p in usuMenuBL.GetAllUsuarioMenu(datoUsuario.UsuarioId)
                            select p.MENU.Tag.ToString()).ToArray();

            System.Windows.Forms.Button[] botones = new System.Windows.Forms.Button[] { 
            this.btnEliminar,this.btnEditar,this.btnNuevo};

            var noVisible = from nv in botones
                            where !permisos.Contains(nv.Tag.ToString())
                            select nv;

            noVisible.ToList().ForEach(nv => nv.Enabled = false);

            CargaData();
        }

        public void CargaData()
        {
            mINISTERIOBindingSource.DataSource = MinisterioBL.GetFromMinisterioByCriterio(txtCriterio.Text);
        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            var nuevo = new FrmMttoMinisterio();
            nuevo.AddMinisteriio();
            nuevo.ShowDialog();
            CargaData();
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            var updateMin = mINISTERIOBindingSource.Current as MINISTERIO;
            if (updateMin != null)
            {
                var _form = new FrmMttoMinisterio();
                _form._ministerio = updateMin;
                _form.UpdateMinisterio();
                _form.ShowDialog();
                CargaData();
            }
            else
            {
                MessageBox.Show("Debe Seleccionar un Ministerio", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            var ministerio = mINISTERIOBindingSource.Current as MINISTERIO;

            var msg = "ERROR";

            try
            {
                if (ministerio != null)
                {
                    var rpt = MessageBox.Show(this,
                    string.Format("¿Desea Eliminar el Ministerio: {0}?", ministerio.Descripcion),
                    "Mensaje",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question);

                    if (rpt == DialogResult.Yes)
                    {
                        MinisterioBL.RemoveMinisterio(ministerio.Id);
                        CargaData();
                    }
                }
                else
                {
                    MessageBox.Show("Debe Seleccionar un Ministerio", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                MessageBox.Show(msg + ministerio.Descripcion + "\n Ya que tiene Células Asociadas", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }


        private void btnBuscar_Click(object sender, EventArgs e)
        {
            CargaData();
        }

        private void txtCriterio_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void txtCriterio_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                CargaData();
            }
        }

        public MINISTERIO CurrentMinisterio { get; set; }

        private void mINISTERIODataGridView_DoubleClick(object sender, EventArgs e)
        {
            if (isExtend == true)
            {
                CurrentMinisterio = mINISTERIOBindingSource.Current as MINISTERIO;
                this.Close();
            }
        }


    }
}
